import request
import json

url_left = 'https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz=MzU1NzEzNjc4MA==&f=json&offset='
url_right = '&count=10&is_ok=1&scene=124&uin=OTkzNDc5ODAw&key=d97826e31f41ebf71fcc7f1fb01452b39c510aaf976e81cb4b7f267f5deda75afff4dab3afc5808e9c89bcd60e0fe28ee431c82a030a22bc998f1a54619f09b7784bfd9b882ea3b45f7faf623d961815a98b700ec9693078ac6c53c0d951ad4986d48d9259afa1174a7fa73de3c7a9314109ae4914b84fa8e7d6d72af245105a&pass_ticket=lq8diyWmMMmLjQD5FZfXtu0b2oUhaqd2RSs%2BSLfUeYVTXlVZWCbXo9so8jYNQKTc&wxtoken=&appmsg_token=1122_xmrxumB0x1Avr%252FCJ3SOzFYnzVTOryauJk7hpHw~~&x5=0&f=json'


def get_url(offset):
    return url_left + str(offset) + url_right


headers = '''
Host: mp.weixin.qq.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63030073)
X-Requested-With: XMLHttpRequest
Accept: */*
Cookie: rewardsn=; wxtokenkey=777; wxuin=993479800; lang=zh_CN; pgv_info=ssid=s5645063921; pgv_pvid=1676347020; dnfqqcomrouteLine=a20210705care; eas_sid=j1h6e226c537J143S7N3002748; _qpsvr_localtk=0.04996191186518084; appmsg_token=1122_wzuo9h81sn7iE%2FMvtA7_dLW43EUmaoBsr2nARJpwJuT21YX7s-Y0BnRQUwj3TAGDnrgQNvG1QIuYehTB; devicetype=iPhoneiOS14.6; version=18000922; pass_ticket=lq8diyWmMMmLjQD5FZfXtu0b2oUhaqd2RSsSLfUeYVTXlVZWCbXo9so8jYNQKTc; wap_sid2=CPiY3dkDErYBeV9IRDhrbGg2QWFlYmNMTTVWT0Z4Y2ZuRWZnNU9yWUZmbXREMHJZdjQ2bk9UZEY3NVF6MEJsbFNiWlk2Um0tb3daVWdKU3hVc3ZtQ2QtTXpvbFpmUEJYb3ZLOEpXeTg4YU1IemVZSVNHX01iTjhFLVh2cjNCT2pOblI3aFY3MEpfbE5qM1drVC1hSzdMc0JBN2d2ZjJUbFRTa0lxNTQxWktLWFc5NlZ2SVA2WV9XUUJJQUFBfn4wsPHPhwY4DUCVTg==
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU1NzEzNjc4MA==&scene=124&uin=OTkzNDc5ODAw&key=d97826e31f41ebf71fcc7f1fb01452b39c510aaf976e81cb4b7f267f5deda75afff4dab3afc5808e9c89bcd60e0fe28ee431c82a030a22bc998f1a54619f09b7784bfd9b882ea3b45f7faf623d961815a98b700ec9693078ac6c53c0d951ad4986d48d9259afa1174a7fa73de3c7a9314109ae4914b84fa8e7d6d72af245105a&devicetype=Windows+10+x64&version=63030073&lang=zh_CN&a8scene=7&pass_ticket=lq8diyWmMMmLjQD5FZfXtu0b2oUhaqd2RSs%2BSLfUeYVTXlVZWCbXo9so8jYNQKTc&fontgear=2
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
'''


def get(offset):
    text = request.request_data(get_url(offset), headers)
    if text == "-1":
        return
    content = json.loads(text)
    if content['errmsg'] != 'ok':
        print("获取微信公众号文章失败，可能是因为你的请求参数有误，请重新获取")
        return []
    if content['msg_count'] == 0:
        print("无数据")
        return []
    data_list = json.loads(content.get('general_msg_list')).get('list')
    list = []
    for item in data_list:
        try:
            item_url = item['app_msg_ext_info']['content_url']
            print(item_url)
            list.append(item_url)
        except BaseException:
            print('jsonError')
    list.extend(get(content['next_offset']))
    return list


def next_data(offset):
    return get(offset)
